#encoding:utf-8
'''
FileName:configutils.py
Creator:王烨
Date:2010.5.22
Comment:配置相关工具类与函数
'''

class Config(dict):
    '''继承自字典，可以像属性一样访问字典项
    '''
    def __getattr__(self, name):
        return self[name]
    
def __TransformDict(configDict):
    '''将存放的配置的Dict转换为Config，方便使用'''
    for k,v in configDict.iteritems():
        if type(v) == dict:
            configDict[k] = __TransformDict(v)
    return Config(configDict)

def CreateSchema(globalsDict, config, env):
    '''根据配置dict生成schema'''
    for k in config:
        def temp(dictName):#解决闭包的作用域问题
            return lambda **args:config[dictName].update(args)
        globalsDict[k] = temp(k)
        env[k] = globalsDict[k]
    return __TransformDict(config)